package com.bestcem.xm.txtanls.dao.impl.mysql;

import cn.hutool.core.collection.CollUtil;
import com.bestcem.xm.common.core.constant.Constants;
import com.bestcem.xm.txtanls.dao.JoinInfoDao;
import com.bestcem.xm.txtanls.dao.mapper.JoinInfoViewpointMapper;
import com.bestcem.xm.txtanls.entity.mysql.JoinInfoViewpointEntity;
import com.bestcem.xm.txtanls.entity.pojo.JoinInfoViewpointDO;
import com.bestcem.xm.txtanls.enums.EmotionKeyEnum;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author liheng
 * @version v1.0
 * @date 2021/6/28 14:18
 */
@Repository
public class JoinInfoDaoImpl implements JoinInfoDao {

    @Resource
    private JoinInfoViewpointMapper joinInfoViewpointMapper;

    /**
     * 查询原话记录id对应的所有原话记录。
     *
     * @param projectId 项目id
     * @param recordIds 原话记录id
     * @return List<JoinInfoEntity>
     * @author jy.zhao
     * @date 2021/5/13 13:58
     **/
    @Override
    public List<JoinInfoViewpointDO> listJoinInfos(String projectId, List<String> recordIds) {
        List<JoinInfoViewpointEntity> entities =
                joinInfoViewpointMapper.listJoinInfos(projectId, recordIds);
        if (CollUtil.isNotEmpty(entities)) {
            return entities.stream().map(entity -> toDO(entity)).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }


    /**
     * 训练结果保存
     *
     * @return String
     * @author gy.liu
     * @date 2021/5/14
     */
    @Override
    public void insertJoinInfo(List<JoinInfoViewpointEntity> joinInfoViewpointEntities) {
        joinInfoViewpointMapper.batchCreate(joinInfoViewpointEntities);
    }

    /**
     * 根据原话id查询原话情感分析记录
     *
     * @param recordId
     * @return
     * @author liheng
     * @date 2021/5/18 20:54
     */
//    @Override
//    public List<JoinInfoViewpointDO> listByRecordId(String recordId) {
//        List<JoinInfoViewpointEntity> entities = joinInfoViewpointMapper.listByRecordId(recordId);
//        if (CollUtil.isNotEmpty(entities)) {
//            return entities.stream().map(entity -> toDO(entity)).collect(Collectors.toList());
//        }
//        return new ArrayList<>();
//    }


    /**
     * 删除分析项目下的全部 join info
     *
     * @param projectId 项目id
     * @return The result of a delete operation
     * @author XiaoYunSong <ys.xiao@idiaoyan.cn>
     * @date 2021/5/21 14:29
     */
    @Override
    public boolean deleteProjectAllJoinInfo(String projectId) {
        return joinInfoViewpointMapper.deleteByProjectId(projectId) > Constants.INT_ZERO;
    }

    @Override
    public List<JoinInfoViewpointDO> listByProjectId(String projectId) {
        List<JoinInfoViewpointEntity> joinInfoViewpointDOS = joinInfoViewpointMapper.listByProjectId(projectId);
        return toDoList(joinInfoViewpointDOS);
    }

    @Override
    public List<String> listIdByViewpointId(List<String> viewpointIds, String projectId,
                                            Integer viewpointScore, Boolean isParent) {
        List<JoinInfoViewpointEntity> result;

        if (Objects.isNull(viewpointScore)) {
            // 无情感查询字段
            result = joinInfoViewpointMapper.listRecordIdByIds(projectId, viewpointIds);
        } else {
            // 父观点处理
            if (Objects.equals(EmotionKeyEnum.MIX.getViewpointScore(), viewpointScore)) {
                // 混合情感过滤
                result = joinInfoViewpointMapper.listRecordIdByIdsMix(projectId, viewpointIds);
            } else {
                // 正向/负向/中性情感过滤
                result = joinInfoViewpointMapper.listRecordIdByIdsAndScore(projectId, viewpointIds, null);

                if (CollUtil.isEmpty(result)) {
                    return Collections.emptyList();
                }
                Map<String, List<JoinInfoViewpointEntity>> collect = result.stream().collect(Collectors.groupingBy(JoinInfoViewpointEntity::getRecordId));
                ArrayList<String> recordIds = new ArrayList<>(result.size());
                // 过滤多余的观点id
                for (Map.Entry<String, List<JoinInfoViewpointEntity>> entry : collect.entrySet()) {
                    String recordId = entry.getKey();
                    boolean temp = true;
                    for (JoinInfoViewpointEntity joinInfoViewpointEntity : entry.getValue()) {
                        // 为子观点并且情感不为查询参数中情感
                        if (viewpointIds.contains(joinInfoViewpointEntity.getViewId()) && !joinInfoViewpointEntity.getViewSenti().equals(viewpointScore)) {
                            temp = false;
                        }
                    }
                    if (temp) {
                        recordIds.add(recordId);
                    }
                }
                return recordIds;
            }
        }

        if (CollUtil.isEmpty(result)) {
            return Collections.emptyList();
        }
        return result.stream().map(JoinInfoViewpointEntity::getRecordId).distinct().collect(Collectors.toList());
    }

    /**
     * JoinInfoViewpointEntity to JoinInfoViewpointDO
     *
     * @param entity
     * @return
     */
    private JoinInfoViewpointDO toDO(JoinInfoViewpointEntity entity) {
        JoinInfoViewpointDO joinInfoViewpointDO = new JoinInfoViewpointDO();
        joinInfoViewpointDO.setCreateTime(entity.getCreateTime());
        joinInfoViewpointDO.setCreatorId(entity.getCreatorId());
        joinInfoViewpointDO.setId(entity.getId());
        joinInfoViewpointDO.setOperatorId(entity.getOperatorId());
        joinInfoViewpointDO.setProjectId(entity.getProjectId());
        joinInfoViewpointDO.setRecordId(entity.getRecordId());
        joinInfoViewpointDO.setRowContent(entity.getRowContent());
        joinInfoViewpointDO.setUpdateTime(entity.getUpdateTime());
        joinInfoViewpointDO.setViewContent(entity.getViewContent());
        joinInfoViewpointDO.setViewId(entity.getViewId());
        joinInfoViewpointDO.setViewSenti(entity.getViewSenti());
        joinInfoViewpointDO.setSample(entity.getSample());
        return joinInfoViewpointDO;
    }

    private List<JoinInfoViewpointDO> toDoList(List<JoinInfoViewpointEntity> entities) {
        if (CollUtil.isEmpty(entities)) {
            return Collections.emptyList();
        }
        ArrayList<JoinInfoViewpointDO> joinInfoViewpointDOS = new ArrayList<>(entities.size());
        for (JoinInfoViewpointEntity entity : entities) {
            joinInfoViewpointDOS.add(toDO(entity));
        }
        return joinInfoViewpointDOS;
    }


    /**
     * JoinInfoViewpointDO to JoinInfoViewpointEntity
     *
     * @param infoViewpointDo
     * @return
     */
    private JoinInfoViewpointEntity toEntity(JoinInfoViewpointDO infoViewpointDo) {
        JoinInfoViewpointEntity joinInfoViewpointEntity = new JoinInfoViewpointEntity();
        joinInfoViewpointEntity.setCreateTime(infoViewpointDo.getCreateTime());
        joinInfoViewpointEntity.setCreatorId(infoViewpointDo.getCreatorId());
        joinInfoViewpointEntity.setId(infoViewpointDo.getId());
        joinInfoViewpointEntity.setOperatorId(infoViewpointDo.getOperatorId());
        joinInfoViewpointEntity.setProjectId(infoViewpointDo.getProjectId());
        joinInfoViewpointEntity.setRecordId(infoViewpointDo.getRecordId());
        joinInfoViewpointEntity.setRowContent(infoViewpointDo.getRowContent());
        joinInfoViewpointEntity.setUpdateTime(infoViewpointDo.getUpdateTime());
        joinInfoViewpointEntity.setViewContent(infoViewpointDo.getViewContent());
        joinInfoViewpointEntity.setViewId(infoViewpointDo.getViewId());
        joinInfoViewpointEntity.setViewSenti(infoViewpointDo.getViewSenti());
        joinInfoViewpointEntity.setSample(infoViewpointDo.getSample());
        return joinInfoViewpointEntity;
    }
}
